 /* ***************图片操作*************** */
 var imgUrlArray = [];
(function(jQuery){
    $.imgUpload = function(){
        //本地相片地址数组
        this.openMeun=function(){
            var as = new Array(2);
            as[0] = "拍照";
            as[1] = "从相册选择";
            uexWindow.actionSheet("选择图片", "取消", as);
			 //弹出选择相片菜单回调
            uexWindow.cbActionSheet = this.actionSheetSuccess;
        }
        this.actionSheetSuccess=function(opId, dataType, data){
            if (data == "0") {
                uexCamera.open('0', '30');
                //相机回调函数
                uexCamera.cbOpen = function(opId, dataType, data){
                    if (dataType == 0) {
                         $.imgUpload().addPic(opId, dataType, data)
                    }
                }
            }
            if (data == "1") {
				param={
	    			'maxfilesize':'20140',
					'with':'',
					'height':''
	    		};
				var paramstr=JSON.stringify(param);
				uexImageBrowser.pick(paramstr);
                uexImageBrowser.cbPick = function(opId, dataType, data){
                    if (dataType == 0) {
                         $.imgUpload().addPic(opId, dataType, data);
                    }
                }
            }
        }
		
		//公共方法
      this.addPic=function(opId, dataType, data) {
	  	    var time = new Date();
            var opid = parseInt(Math.random() * 1000);
            var ms = time.getTime();
            var picData = data;
            var picId = ms + opid;
            var img = {
                "fileId": picId,
                "fileData": picData
            };
            //加入本地文件列表
           imgUrlArray.push(img);
           $.imgUpload().createImgDiv(picId);
          document.getElementById("img" + picId).src = data;
        }
        //存放图片的div
        this.createImgDiv=function(id){
            var idex = $("#pictures div").length;
            if (idex == 2) {
                $("#addImg").remove();
            }
            var appendImgDiv = '<li class="lis-th ub-img uba" style="width:31.5%;height:5.5em">'+
            '<img id="img'+id+'" style="width:100%;height:100%;" onclick="preview(this.src)"/>'+
            '<div class="deleteImg ub-img" id="'+id+'" style="top:-5.9em;right:-2.4em;z-index:1;position:relative;" onclick=\' $.imgUpload().deleteImg(this)\'>&nbsp;</div></li>'
            $("#pictures").prepend(appendImgDiv);
        }
        
        //删除选中的图片
        this.deleteImg = function(obj){
            var idex = $("#pictures div").length;
            if (idex == 3) {
                $("#pictures").append('<li id="addImg" class="lis-th ub-img Img" style="width:32%;height:5.5em;" onclick="$.imgUpload().openMeun()"></li>');
            }
            $(obj).parent().remove();
			for(var i=0;i<imgUrlArray.length;i++){
				var json = imgUrlArray[i];
				if(json.fileId==obj.id){
					imgUrlArray.remove(i);
				}
			}
        }
		
		
		/**
		 * 图片上传
		 * @param {Object} file
		 */
        this.fileUp = function(){
			$.iBoxWait();
			var affairLoc="";
			$('[name="che1"]:checked').each(function() {
				var zhi = $(this).val()+",";
				affairLoc +=zhi;
			});
			if(affairLoc.length==0){
				 $.iBoxMsg("请至少选择一个事件提交");
				 return;
			}
			var affairRemark = $("#affairRemark").val();
			if(affairRemark==""){
				 $.iBoxMsg("请对申请事件进行描述");
				 return;
			}
            
            if (imgUrlArray.length == 0) {
                upload(",");
            }
            else {
                var jsonStr = "";
                imgUp(0);
                function imgUp(i){
					console.log(imgUrlArray.length);
                    if (i < imgUrlArray.length) {
                        var opid = imgUrlArray[i].fileId+"";
						opid = opid.substr(0,10);
                        var url = queryUrl + 'flieUpload';
                        uexXmlHttpMgr.onData = function(_opid, status, result){
                            var resultJson = JSON.parse(result);
                            if (resultJson.status == "0") {
                                jsonStr += resultJson.arry + ",";
                            }
                            else {
                                $.iBoxMsg("图片上传失败,请重试");
                                return;
                            }
                            uexXmlHttpMgr.close(_opid);
							 i++;
                             imgUp(i);
                        }
                        
                        uexXmlHttpMgr.open(opid, "POST", url, 0);
                        try {
                            uexXmlHttpMgr.setPostData(opid, "1", "file", imgUrlArray[i].fileData);
                        } 
                        catch (e) {
                        }
                        uexXmlHttpMgr.send(opid);
                    }
                    else {
                        upload(jsonStr);
                    }
                }
            }
        };


	/**
		 * 图片上传
		 * @param {Object} file
		 */
        this.uploadImgs = function(status, strMessge){
            $.iBoxWait();
            if (imgUrlArray.length == 0) {
                comimtDate(",", status, strMessge);
            }
            else {
                var jsonStr = "";
                imgUp(0);
                function imgUp(i){
                    if (i < imgUrlArray.length) {
                        var opid = imgUrlArray[i].fileId+"";
						opid = opid.substr(0,10);
                        var url = queryUrl + 'flieUpload';
                        uexXmlHttpMgr.onData = function(_opid, status, result){
                            var resultJson = JSON.parse(result);
                            if (resultJson.status == "0") {
                                jsonStr += resultJson.arry + ",";
                            }
                            else {
                                $.iBoxMsg("图片上传失败,请重试");
                                return;
                            }
                            uexXmlHttpMgr.close(_opid);
                            i++;
                            imgUp(i);
                        }
                        
                        uexXmlHttpMgr.open(opid, "POST", url, 0);
                        try {
                            uexXmlHttpMgr.setPostData(opid, "1", "file", imgUrlArray[i].fileData);
                        } 
                        catch (e) {
                        }
                        uexXmlHttpMgr.send(opid);
                    }
                    else {
                        comimtDate(jsonStr, status, strMessge);
                    }
                }
            }
        };
        return this;
    }
})(jQuery);
 /**
         *删除数组指定下标或指定对象
         */
        Array.prototype.remove = function(obj){
            for (var i = 0; i < this.length; i++) {
                var temp = this[i];
                if (!isNaN(obj)) {
                    temp = i;
                }
                if (temp == obj) {
                    for (var j = i; j < this.length; j++) {
                        this[j] = this[j + 1];
                    }
                    this.length = this.length - 1;
                }
            }
        }